﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace YouHaoApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class VehicleController : ControllerBase
    {
        YouHaoContext _ctx;
        public VehicleController(YouHaoContext ctx)
        {
            _ctx = ctx;
        }

        // GET api/<VehicleController>/5
        [HttpGet("{id}")]
        public VehicleModel Get(int id)
        {
            var m = _ctx.Vehicles.Find(id);
            VehicleModel r = new VehicleModel { UserId = (int)m.UserId, VehicleName = m.VehicleName,VehicleId=m.VehicleId };
            return r;
        }

        // POST api/<VehicleController>
        [HttpPost]
        public void Post([FromBody] VehicleModel value)
        {
            Vehicle v = new Vehicle { UserId=value.UserId,VehicleName=value.VehicleName};
            _ctx.Vehicles.Add(v);
            _ctx.SaveChanges();
        }

        // PUT api/<VehicleController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
            Vehicle v = _ctx.Vehicles.Find(id);
            v.VehicleName = value;
            _ctx.SaveChanges();
        }

        // DELETE api/<VehicleController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            var v = _ctx.Vehicles.Find(id);
            if (v != null)
            {
                _ctx.Remove(v);
                _ctx.SaveChanges();
            }

        }
    }
}
